package com.mf.all.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.all.exception.GraceException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.mf.all.entity.dto.DtoCustomer;
import com.mf.all.entity.req.search.customer.SearchReqCustomer;
import com.mf.all.mapper.MapperCustomer;
import com.mf.all.service.ServiceCustomer;


@Service
public class ImplServiceCustomer extends ServiceImpl<MapperCustomer, DtoCustomer> implements ServiceCustomer {


    @Override
    public LambdaQueryWrapper<DtoCustomer> fillParams(SearchReqCustomer params) {
        final LambdaQueryWrapper<DtoCustomer> wrapper = loadQw();
        if (StringUtils.isNotBlank(params.getKey())) {
            wrapper.and(w -> {
                w.like(DtoCustomer::getCName, params.getKey())
                        .or()
                        .like(DtoCustomer::getShortName, params.getKey())
                        .or()
                        .like(DtoCustomer::getDescription, params.getKey())
                        .or()
                        .like(DtoCustomer::getIndexCode, params.getKey());
            });
        }
        wrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), DtoCustomer::getStatus, params.getStatus())
                .eq(StringUtils.isNoneBlank(params.getType()), DtoCustomer::getType, params.getType())
                .eq(StringUtils.isNoneBlank(params.getLevel()), DtoCustomer::getLevel, params.getLevel())
                .orderByDesc(DtoCustomer::getId)
                .orderByDesc(DtoCustomer::getSort);
        return wrapper;
    }

    /**
     * 查询是否有该客户
     */
    @Override
    public void isExistById(Integer id) {
        final DtoCustomer customer = getById(id);
        if (ObjectUtils.isEmpty(customer)) {
            GraceException.display("客户不存在");
        }
    }
}